
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author personal
 */
public class SplashScreen extends GameCanvas implements Runnable {

    private BTIMidlet midlet;
    //---------------------------------------------------
    //sprite
    private Sprite spBackground;
    private Sprite spLoading;
    private LayerManager lmgr;
    //---------------------------------------------------
    //image
    private Image imgSplash;
    private String pathSplash;
    //---------------------------------------------------
    //xy
    private int xLoading = 0;
    private int yLoading = 0;
    private int xTitle = 0;
    private int yTitle = 0;
    private int xString = 0;
    private int yString = 0;
    //---------------------------------------------------
    //thread
    private boolean running = false;
    private int loop = 0;
    private Thread thread = null;
    private int speedGame = 30;
    
    //boolean
    private boolean isPaused = false;
    private boolean isNotSupported = false;

    public SplashScreen(BTIMidlet midlet) {
        super(true);
        this.midlet = midlet;
        setFullScreenMode(true);

        //5200  - 128 x 160
        //5300  - 240 x 320
        BTIMidlet.WIDTH = getWidth();
        BTIMidlet.HEIGHT = getHeight();
        //BTIMidlet.WIDTH = 128;
        //BTIMidlet.HEIGHT = 160;


        if (BTIMidlet.WIDTH == 128) {
            pathSplash = "/splash128x160.png";
        } else if (BTIMidlet.WIDTH == 240) {
            pathSplash = "/splash240x320.png";
        } else {
            isNotSupported = true;
        }
        System.out.println(BTIMidlet.WIDTH + "x"
                + BTIMidlet.HEIGHT);

        try {
            //animated sprite
            spBackground = new Sprite(Image.createImage(pathSplash));
            spLoading = new Sprite(Image.createImage("/loading.png"),
                    50, 50);

            spBackground.setPosition(0, 0);

            lmgr = new LayerManager();
            lmgr.append(spBackground);
            lmgr.append(spLoading);

        } catch (IOException ex) {
        }
    }

    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            if (loop > 99) {
                running = false;
            }
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }

        if (loop > 20) {
            if (isNotSupported) {
                thread.interrupt();
                midlet.showNotSupported();
            } else {
                midlet.showMenuCanvas();
            }
        }

    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        //System.out.println(spBalloon.getFrameSequenceLength());
        /*
        if(MobileMonstersMIDlet.LANDSCAPE==0){
        setPortraitDevice();
        }
         * 
         */
        lmgr.paint(g, 0, 0);
        loop++;
        //flush off-screen buffer to display
        flushGraphics();
    }

    private void setPortraitDevice() {
        spBackground.setPosition(0, 0);
        spBackground.setTransform(Sprite.TRANS_ROT90);
    }

    /*
    private void dismiss() {
    if (isShown()) {
    host.showMenuCanvas();
    }
    }
     * 
     */
    protected void sizeChanged(int w, int h) {
        //super.sizeChanged(w, h);
        /*
        if (getWidth() < getHeight()) {
        if (getWidth() > acuanDisable) {
        isDisablePortrait = true;
        //System.out.println("PAUSED OY!");
        paused();
        } else {
        isDisablePortrait = false;
        }
        } else {
        isDisablePortrait = false;
        updatePositions();
        Graphics g = getGraphics();
        drawDisplay(g);
        if (isPaused) {
        int xResume = getWidth() - imgBtnResume.getWidth();
        int yResume = getHeight() - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
        Graphics.TOP | Graphics.LEFT);
        btnResume.paint(g);
        }
        }
        updatePositions();
         *
         */
    }

    /*
    private void updatePositions() {
    xTitle = getWidth() / 2 - (imgTitle.getWidth() / 2);
    yTitle = getHeight() / 2 - (imgTitle.getHeight());
    xString = getWidth() / 2;
    yString = 100; //spBalloon.getY() + (spBalloon.getHeight() / 4);
    }
     * 
     */
    protected void hideNotify() {
        //super.hideNotify();
        //hideNotify();
        paused();
    }

    protected void showNotify() {
        //super.showNotify();
        //showNotify();
    }

    private void paused() {
        /*
        isPaused = true;
        running = false;
         * 
         */
    }

    protected void pointerPressed(int x, int y) {
    }

    public void stop() {
        thread.interrupt();
        running = false;
    }
}
